#include<vector>
#include<iostream>

using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	ListNode* reverseList(ListNode* head) {
		if (!head) return NULL;
		ListNode *r = head;
		vector<ListNode *> list;
		while (r) {
			list.push_back(r);
			r = r->next;
		}
		int index = list.size() - 1;
		ListNode *t=list[index--],*newhead=t;
		while (index>=0) {
			ListNode *r = list[index--];
			t->next = r;
			t = t->next;
		}
		t->next = NULL;
		return newhead;
	}
};

void main() {
	ListNode n1(1), n2(2), n3(3), n4(4), n5(5);
	//n1.next = &n2, n2.next = &n3, n3.next = &n4, n4.next = &n5;
	//n1.next = &n2;
	Solution s;
	s.reverseList(&n1);
}